package com.filyar.htyd.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.filyar.htyd.common.Result;
import com.filyar.htyd.entity.CompareResult;
import com.filyar.htyd.service.ICompareResultService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author filyar
 * @since 2024-10-24
 * @e-mail root@filyar.com
 */
@RestController
@RequestMapping("/compareResult")
public class CompareResultController {

    @Resource
    private ICompareResultService compareResultService;

    private final String now = DateUtil.now();

    @PostMapping
    public Result save(@RequestBody CompareResult compareResult) {
        compareResultService.saveOrUpdate(compareResult);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        compareResultService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        compareResultService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(compareResultService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String a,
                           @RequestParam(defaultValue = "") String z,
                           @RequestParam double aLikePercent,
                           @RequestParam double zLikePercent,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        LambdaQueryWrapper<CompareResult> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(CompareResult::getALikePercent);
        if (!"".equals(a)) {
            queryWrapper.like(CompareResult::getA, a);
        }
        if (!"".equals(z)) {
            queryWrapper.like(CompareResult::getZ, z);
        }
        if (aLikePercent > 0.5){
            if (aLikePercent == 1){
                queryWrapper.eq(CompareResult::getALikePercent, aLikePercent);
            }else {
                queryWrapper.lt(CompareResult::getALikePercent, aLikePercent);
            }
        }
        if (zLikePercent > 0.5){
            if (zLikePercent == 1){
                queryWrapper.eq(CompareResult::getZLikePercent, zLikePercent);
            }else
                queryWrapper.lt(CompareResult::getZLikePercent, zLikePercent);
        }
        return Result.success(compareResultService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }



}

